<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2018/7/17
  Time: 15:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<div class="container-fluid">
    <link rel="stylesheet" href="/static/dist/lib/chosen/chosen.css">
    用户修改
    <form id="user-form-add" name="resourceForm" enctype="multipart/form-data" method="post" >
        <input type="hidden" name="id" value="${user.id}">
        <fieldset>
        <div class="form-group">
            <label class="label">用户名称</label>
            <input type="text" name="name" class="form-control" placeholder="用户名称" value="${user.name}"/>
        </div>

        <div class="form-group">
            <label class="label">登陆账号</label>
            <input type="text" name="loginName" class="form-control" placeholder="登陆账号" value="${user.loginName}"/>
        </div>

        <div class="form-group">
            <label class="label">年龄</label>
            <input type="number" class="form-control" name="age" id="age" placeholder="年龄" value="${user.age}"/>
        </div>

        <div class="form-group">
            <label class="label">电话</label>
            <input class="chosen-icons form-control" name="phone" id="phone" placeholder="电话" value="${user.phone}"/>
        </div>

        <div class="form-group">
            <label class="label">性别</label>
            <div style="border: 1px solid #ccc; padding: 10px">
                <label class="radio-inline">
                    <input type="radio" value="1" name="sex" id="gender" <c:if test="${user.sex == 1}">checked</c:if>> 男
                </label>
                <label class="radio-inline">
                    <input type="radio" value="0" name="sex" checked> 女
                </label>
            </div>
        </div>

        <div class="form-group">
            <label class="label">角色</label>
            <select class="form-control" name="role" id="role" placeholder="角色" >
                <option value="">无</option>
            </select>
        </div>

        <div class="form-group">
            <label class="label">状态</label>
            <div style="border: 1px solid #ccc; padding: 10px">
                <div class="switch">
                    <input type="checkbox" name="status" value="0" <c:if test="${user.status == 0}">checked</c:if>/>
                    <label><c:if test="${user.status == 0}">正常</c:if><c:if test="${user.status == 1}">停用</c:if></label>
                </div>
            </div>
            <%--<input class="form-control" name="status" id="status" placeholder="状态" />--%>
        </div>

        <button type="button" class="btn btn-primary" id="user_btn_save">保 存</button>
        </fieldset>
    </form>
    <script src="/static/dist/lib/chosen/chosen.js"></script>
    <script type="application/javascript">

        $('input[name=status]').on('click', function () {
            if($(this).prop("checked")) {
                $(this).next().html("正常");
            }else {
                $(this).next().html("停用");
            }
        });

        /**
         * 初始化上级资源
         */
        $.post('/admin/role/getRolesByUId', {}, function(res) {
            var oldData = '${user.role}';
            var html = "";
            console.log(oldData);
            console.log(res.data);
            for(index in res.data){
                if(res.data[index]){
                    var optionSelected = (oldData == res.data[index].id) ? ('selected') : ('');

                    html += "<option value='" + res.data[index].id + "'" + optionSelected + ">" + res.data[index].name + "</option>";
                }
            }
            $('#role').append(html);
            $('#role').chosen({
                no_results_text: '没有找到',    // 当检索时没有找到匹配项时显示的提示文本
                disable_search_threshold: 10, // 10 个以下的选择项则不显示检索框
                search_contains: true         // 从任意位置开始检索
            });
        });


        /**
         * 保存按钮
         */
        $('#user_btn_save').on('click', function () {

            var _this = this;
            var data = $('#user-form-add').serializeArray();
            var params = {};
            for (index in data) {
                params[data[index].name] = data[index].value;
            }
            $(_this).html('<i class="icon icon-spin icon-spinner-snake"></i>正在保存……')
            $('#resource-form-add fieldset').attr('disabled', true);
            $.post('/admin/user/update', params, function (res) {
                if(res.code == 200 && res.status){
                    new $.zui.Messager('保存成功。', {
                        type: 'success',
                        icon: 'check',//smile
                        close: true,
                        time: 0,
                        placement: 'center' // 定义显示位置
                    }).show();
                    setTimeout(function () {
                        // 页面跳转
                        $('body').hrefClick('/admin/user/index');
                    }, 2000);
                }else{
                    new $.zui.Messager('保存失败，请检查或修改内容后重试。', {
                        type: 'danger',
                        icon: 'frown',//smile
                        close: true,
                        placement: 'center' // 定义显示位置
                    }).show();
                    $(_this).html('保 存');
                    $('#resource-form-add fieldset').attr('disabled', false);
                }
            });
        });

    </script>
</div>